import {AbilityModel} from "../../controller/model/AbilityModel";
import Configer from "../../frame/apiModel/config/Configer";
import {FinalKey} from "../../constant/FinalConstant";
import {UnitController} from "../../controller/UnitController";
import {Logger} from "../../frame/apiModel/config/Logger";
import {UnitModel} from "../../frame/apiModel/adapterModel/model3/UnitModel";
import {ItemModel} from "../../frame/apiModel/adapterModel/model3/ItemModel";
import {PlayerModel} from "../../frame/apiModel/adapterModel/model4/PlayerModel";
import {LocModel} from "../../frame/apiModel/base/LocModel";
import {PlayerFactory} from "../../frame/PlayerFactory";
import {DataRouterUtil} from "../../utils/router/DataRouterUtil";
import {CodeUtil} from "../../frame/codeAdapter/CodeUtil";
import {AttributeEnum, ItemType, UnitType} from "../../frame/dataSource/DataModel";
import {Util} from "../../utils/Util";
import {AbilityUtil} from "./AbilityUtil";
import UnitUtil from "../../utils/UnitUtil";
import {DataRouter} from "../../frame/dataSource/DataRouter";
import {EffectModel} from "../../frame/apiModel/adapterModel/model2/EffectModel";
import {ModelFactory} from "../../frame/apiModel/adapterModel/model5/ModelFactory";
import {TimeControllerOpt} from "../../controller/model/ControllerOpt";
import {TimeController} from "../../controller/TimeController";
import MathUtil from "../../frame/codeAdapter/MathUtil";
import {GlobalEnv} from "../../utils/GlobalEnv";
import {MaterialInfo} from "../../type/Type";
import {TaskController} from "../../controller/TaskController";
import {ModelType} from "../../frame/apiModel/adapter/adapterInterface/model5/IModelFactory";
import {ConfigKey} from "../../constant/ConfigConstant";

export default class OneClickFixupSkillModel extends AbilityModel {
    abilityId: string = Configer.getFinal(FinalKey.oneClickFixup) as string;

    actionFun = (activeUnit: UnitModel, activeItem: ItemModel, targetUnit: UnitModel, targetItem: ItemModel, activePlayer: PlayerModel, targetPlayer: PlayerModel, spellId: string, spellLoc: LocModel)  => {
        let success;
        success = this.oneClickFixup(activeUnit, activePlayer);
        return success;
    }

    oneClickFixup(activeUnit: UnitModel, player: PlayerModel) {
        const playerId = player.playerId;
        const fixUpRanges: string[] = Configer.getFinal(FinalKey.fixUpRanges);
        const rangeName = fixUpRanges[playerId];
        if (rangeName) {
            const rectModel = ModelFactory.getInstance().getRectModel(rangeName)
            const startLoc = player.startLocation;
            let bMove = false;

            const ablityItems: ItemModel[] = [];
            const rewardBoxItems: ItemModel[] = [];
            const equipItems: ItemModel[] = [];
            const otherItems: ItemModel[] = [];

            ModelFactory.getInstance().mapItemsInRect(rectModel, (item) => {
                if (item.show) {
                    const type = item.template.item_type;
                    switch (type) {
                        case ItemType.ability: {
                            ablityItems.push(item);
                            break;
                        }
                        case ItemType.equip: {
                            equipItems.push(item);
                            break;
                        }
                        case ItemType.rewardBox: {
                            rewardBoxItems.push(item);
                            break;
                        }
                        default : {
                            otherItems.push(item);
                            break;
                        }
                    }
                }
            })


            const ablityItemsSqrt = MathUtil.sqrt(ablityItems.length);
            const ablityItemsWidth = MathUtil.ceil(ablityItemsSqrt);

            const equipItemsSqrt = MathUtil.sqrt(equipItems.length);
            const equipItemsWidth = MathUtil.ceil(equipItemsSqrt);

            const rewardBoxItemsSqrt = MathUtil.sqrt(rewardBoxItems.length);
            const rewardBoxItemsWidth = MathUtil.ceil(rewardBoxItemsSqrt);

            const otherItemsSqrt = MathUtil.sqrt(otherItems.length);
            const otherItemsWidth = MathUtil.ceil(otherItemsSqrt);


            const dropSpace: number = Configer.getConfig(ConfigKey.dropSpace);
            const maxSpace = MathUtil.max(ablityItemsWidth, equipItemsWidth, rewardBoxItemsWidth, otherItemsWidth);

            const retArr: ItemModel[][] = [];
            if (ablityItems.length > 0) {
                retArr.push(ablityItems);
            }
            if (equipItems.length > 0) {
                retArr.push(equipItems);
            }
            if (rewardBoxItems.length > 0) {
                retArr.push(rewardBoxItems);
            }
            if (otherItems.length > 0) {
                retArr.push(otherItems);
            }

            const locs = TaskController.getInstance().getScatteredPoint(retArr.length, startLoc,dropSpace * maxSpace);

            for (let i = 0; i < locs.length; i++) {
                const loc = locs[i];
                const ret = retArr[i];
                const tempLocs = TaskController.getInstance().getScatteredPoint(ret.length, loc, dropSpace);

                for (let j = 0; j < ret.length; j++) {
                    const item = ret[j];
                    const tempLoc = tempLocs[j];
                    item.loc = tempLoc;

                    bMove = true;
                }
            }

            if (bMove) {
            } else {
                Logger.toastError('区域内没有物品', player)
            }
        } else {
            Logger.toastError('作者未配置当前玩家一键整理区域，请联系作者', player);
            return false;
        }
        return true;
    }
}